package servicebctp

import (
	"context"
	"github.com/aberic/gnomon"
	"github.com/pkg/errors"
	"google.golang.org/grpc"
	"gykjgit.dccnet.com.cn/chain/proto/chain/bctp"
)

// BlockByHeight BlockByHeight
func BlockByHeight(url string, in *bctp.ReqBlockHeight) (*bctp.RespBlockHeight, error) {
	resp, err := gnomon.GRPCRequestPools(url, func(conn *grpc.ClientConn) (i interface{}, err error) {
		// 创建grpc客户端
		cli := bctp.NewBlockClient(conn)
		//客户端向grpc服务端发起请求
		return cli.Height(context.Background(), in)
	})
	if nil == err {
		if response, ok := resp.(*bctp.RespBlockHeight); ok {
			return response, nil
		}
		return nil, errors.New("get block height failed, can not tarns interface to response")
	}
	return nil, errors.Wrap(err, "get block height failed")
}

// BlockInfo BlockInfo
func BlockInfo(url string, in *bctp.ReqBlockInfo) (*bctp.RespBlockInfo, error) {
	resp, err := gnomon.GRPCRequestPools(url, func(conn *grpc.ClientConn) (i interface{}, err error) {
		// 创建grpc客户端
		cli := bctp.NewBlockClient(conn)
		//客户端向grpc服务端发起请求
		return cli.Info(context.Background(), in)
	})
	if nil == err {
		if response, ok := resp.(*bctp.RespBlockInfo); ok {
			return response, nil
		}
		return nil, errors.New("block info failed, can not tarns interface to response")
	}
	return nil, errors.Wrap(err, "block info failed")
}
